<template>
  <view v-if="visible" class="cart-modal" @tap="$emit('close')">
    <view class="cart-content" @tap.stop>
      <view class="cart-header">
        <text class="cart-title">购物车</text>
        <view class="cart-clear" @tap="$emit('clear')">
          <uni-icons type="trash" size="18" color="#999"></uni-icons>
          <text class="clear-text">清空</text>
        </view>
      </view>
      
      <scroll-view class="cart-list" scroll-y>
        <view 
          v-for="item in cartItems" 
          :key="item.specKey"
          class="cart-item"
        >
          <image :src="item.image" class="item-image"></image>
          <view class="item-info">
            <text class="item-name">{{ item.name }}</text>
            <text v-if="item.specs" class="item-specs">{{ item.specs }}</text>
            <view class="item-bottom">
              <text class="item-price">¥{{ item.price }}</text>
              <view class="item-actions">
                <view class="action-btn" @tap="$emit('decrease', item)">
                  <uni-icons type="minus" size="16" color="#00C853"></uni-icons>
                </view>
                <text class="item-count">{{ item.count }}</text>
                <view class="action-btn increase" @tap="$emit('increase', item)">
                  <uni-icons type="plus" size="16" color="#fff"></uni-icons>
                </view>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script setup lang="ts">
interface CartItem {
  id: number;
  name: string;
  price: number;
  count: number;
  image: string;
  specs?: string;
  specKey: string;
}

interface Props {
  visible: boolean;
  cartItems: CartItem[];
}

defineProps<Props>();
defineEmits(['close', 'clear', 'increase', 'decrease']);
</script>

<style scoped>
.cart-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0,0,0,0.5);
  z-index: 999;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.cart-content {
  background-color: #fff;
  max-height: 60vh;
  border-radius: 20rpx 20rpx 0 0;
  display: flex;
  flex-direction: column;
}

.cart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.cart-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.cart-clear {
  display: flex;
  align-items: center;
  gap: 5rpx;
}

.clear-text {
  font-size: 26rpx;
  color: #999;
}

.cart-list {
  flex: 1;
  padding: 20rpx 30rpx;
}

.cart-item {
  display: flex;
  gap: 20rpx;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.cart-item:last-child {
  border-bottom: none;
}

.item-image {
  width: 120rpx;
  height: 120rpx;
  border-radius: 8rpx;
  flex-shrink: 0;
}

.item-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.item-name {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 5rpx;
}

.item-specs {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 10rpx;
}

.item-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item-price {
  font-size: 28rpx;
  color: #ff3b30;
  font-weight: bold;
}

.item-actions {
  display: flex;
  align-items: center;
  gap: 15rpx;
}

.action-btn {
  width: 44rpx;
  height: 44rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2rpx solid #00C853;
  background-color: #fff;
}

.action-btn.increase {
  background-color: #00C853;
  border: none;
}

.item-count {
  font-size: 26rpx;
  color: #333;
  min-width: 40rpx;
  text-align: center;
}
</style>